package com.sinosoft.dsp.security.account;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import com.sinosoft.dsp.platform.utils.MessageBundles;
import com.sinosoft.dsp.system.entity.ISecurityUser;

/**
 * 用于检验提交的用户和密码是否匹配
 * 
 * @author 潘巍（PeterPan）
 * @since 2011-5-30 下午11:36:59
 * 
 */
@FacesValidator("currentPassword")
public class CurrentPasswordValidator implements Validator {
	@ManagedProperty(value = "#{messageBundles}")
	private MessageBundles messageBundles;

	@Authenticated
	@ManagedProperty(value = "#{securityUser}")
	private ISecurityUser currentUser;

	public void validate(final FacesContext ctx, final UIComponent comp,
			final Object value) throws ValidatorException {
		String currentPassword = (String) value;
		if ((currentUser.getPassword() != null)
				&& !currentUser.getPassword().equals(currentPassword)) {
			throw new ValidatorException(new FacesMessage(messageBundles
					.getValue("account.current.passwordsDoNotMatch")));
		}
	}

}
